package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.exception.BusinessException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired(required = false)
    private SetmealMapper setmealMapper;

    @Autowired(required = false)
    private SetmealDishMapper setmealDishMapper;

    @Autowired(required = false)
    private CategoryMapper categoryMapper;

    @Override
    @Transactional
    public boolean save(SetmealDto setmealDto) {
        //1.完善套餐数据(状态 创建时间 更新时间)
        setmealDto.setStatus(1);
        setmealDto.setCreateTime(LocalDateTime.now());
        setmealDto.setUpdateTime(LocalDateTime.now());

        //2.调用数据访问层(获取主键)
        setmealMapper.save(setmealDto);

        //3.遍历套餐菜品关系列表完善数据
        List<SetmealDish> setmealDishList=setmealDto.getSetmealDishes().stream().map(setmealDish -> {
            //设置套餐主键,创建人,创建时间,更新人,更新时间,排序
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDish.setCreateUser(setmealDto.getCreateUser());
            setmealDish.setCreateTime(LocalDateTime.now());
            setmealDish.setUpdateUser(setmealDto.getUpdateUser());
            setmealDish.setUpdateTime(LocalDateTime.now());
            setmealDish.setSort(0);

            return setmealDish;
        }).collect(Collectors.toList());

        //4.调用数据访问层批量插入套餐菜品列表数据
        setmealDishMapper.save(setmealDishList);

        return true;
    }

    /**
     * 套餐分页查询数据
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<SetmealDto> findByPage(Integer page, Integer pageSize, String name) {
        //1.pageHelper设置查询第几页和每页大小
        PageHelper.startPage(page,pageSize);

        //2.执行查询所有的套餐数据
        List<Setmeal> setmealList = setmealMapper.findAll(name);

        //3.封装成PageInfo对象
        PageInfo<Setmeal> pageInfo=new PageInfo<>(setmealList);

        //4.处理当前页数据列表中每个套餐setmeal对象
        List<SetmealDto> setmealDtoList=setmealList.stream().map((setmeal -> {
            SetmealDto setmealDto = new SetmealDto();

            //将setmeal套餐对象的数据赋值给setmealDto里面属性数据
            BeanUtils.copyProperties(setmeal,setmealDto);
            //获取类别名称
            Category category = categoryMapper.findById(setmealDto.getCategoryId());
            //将类别名称赋值给setmealDto
            setmealDto.setCategoryName(category.getName());

            return setmealDto;

        })).collect(Collectors.toList());

        //封装成前端需要的Page<SetmealDto>对象
        Page<SetmealDto> pageObj = new Page<>();
        pageObj.setRecords(setmealDtoList);
        pageObj.setTotal(pageInfo.getTotal());

        //返回
        return pageObj;
    }

    /**
     * 删除套餐业务方法
     *
     * @param ids
     * @param updateUser
     * @return
     */
    @Override
    public boolean deleteByIds(List<Long> ids, Long updateUser) {
        //1.判断传递过来的套餐id列表查询是否存在启用的套餐
        //查询启动的个数,个数大于0代表存在,否则不存在
        Integer count = setmealMapper.countByIds(ids);

        //2.如果存在,抛出异常
        if (count>0){
            throw new BusinessException("无法删除起售的套餐");
        }

        //3.调用数据访问层进行批量删除,传递修改人,修改时间
        //批量删除套餐菜品关系数据
        setmealDishMapper.deleteBySetmealIds(ids,updateUser,LocalDateTime.now());

        //批量删除套餐
        setmealMapper.deleteByIds(ids,updateUser,LocalDateTime.now());

        //返回
        return true;
    }

    /**
     * 根据套餐类别查询套餐
     *
     * @param categoryId
     * @param status
     * @return
     */
    @Override
    public List<Setmeal> list(Long categoryId, Integer status) {
        return setmealMapper.list(categoryId,status);
    }
}
